package me.firstapp.service.shop.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import me.firstapp.common.exception.ServiceException;
import me.firstapp.common.exception.StatusHouse;
import me.firstapp.common.utils.StrUtils;
import me.firstapp.module.bbs.Member;
import me.firstapp.module.shop.Address;
import me.firstapp.repository.bbs.MemberRepository;
import me.firstapp.repository.shop.AddressRepository;
import me.firstapp.service.shop.AddressService;

@Service
public class AddressServiceImpl implements AddressService {

	@Autowired
	private MemberRepository memberRepository;

	@Autowired
	private AddressRepository addressRepository;

	@Override
	public Address addUpdateAddress(Long memberId, String name, String mobile, String province, String city,
			String district, String detail, Long apiClientId) {
		if (memberId == null) {
			throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "用户id");
		}
		Member member = (Member) memberRepository.findMemberById(memberId, apiClientId);
		if (member == null) {
			throw new ServiceException(StatusHouse.MEMBER_NOT_FOUND);
		}
		Address address = addressRepository.findAddressByMemberId(memberId, apiClientId);
		if (address == null) {
			if (StrUtils.isNULL(name)) {
				throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "收货人姓名");
			}
			if (StrUtils.isNULL(province)) {
				throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "省");
			}
			if (StrUtils.isNULL(city)) {
				throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "市");
			}
			if (StrUtils.isNULL(district)) {
				throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "区");
			}
			if (StrUtils.isNULL(detail)) {
				throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "详细地址");
			}
			address = new Address();
			address.setMember(member);
			address.setName(name);
			address.setMobile(mobile);
			address.setProvince(province);
			address.setCity(city);
			address.setDistrict(district);
			address.setDetail(detail);
			address.setApiClientId(apiClientId);
			addressRepository.save(address);
		} else {
			if (StrUtils.isNotNULL(name)) {
				address.setName(name);
			}
			if (StrUtils.isNotNULL(mobile)) {
				address.setMobile(mobile);
			}
			if (StrUtils.isNotNULL(province)) {
				address.setProvince(province);
			}
			if (StrUtils.isNotNULL(city)) {
				address.setCity(city);
			}
			if (StrUtils.isNotNULL(district)) {
				address.setDistrict(district);
			}
			if (StrUtils.isNotNULL(detail)) {
				address.setDetail(detail);
			}
			addressRepository.update(address);
		}

		return address;
	}

	@Override
	public Address findAddressByMemberId(Long memberId, Long apiClientId) {
		if (memberId == null) {
			throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "用户id");
		}
		return addressRepository.findAddressByMemberId(memberId, apiClientId);
	}

}
